﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace WebApi
{

    /// <summary>
    /// 验证数据的格式，按照自定义的值类型返回
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
    public class ReActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext context)
        {
            base.OnActionExecuted(context);
        }

        public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            foreach (var item in context.ActionArguments)
            {
                try
                {
                    var obj = item.Value;
                    if(obj!=null)
                    {
                        var type = obj.GetType();
                        if (type.BaseType.Name.Equals("Object"))
                        {
                            string content = JsonConvert.SerializeObject(obj);
                            if (content.Contains("<script>"))
                            {
                                context.HttpContext.Response.StatusCode = 200;
                                context.HttpContext.Response.ContentType = "application/json;charset=utf-8";
                                context.Result = new JsonResult(new ResponseData
                                {
                                    ErrorCode = 1,
                                    Message = $"操作失败！内容包含特殊字符。",
                                    DataList = null
                                }
                                );
                                return Task.CompletedTask;
                            }
                        }
                        else
                        {
                            if (obj != null)
                            {
                                if (obj.ToString().Contains("<script>"))
                                {
                                    context.HttpContext.Response.StatusCode = 200;
                                    context.HttpContext.Response.ContentType = "application/json;charset=utf-8";
                                    context.Result = new JsonResult(new ResponseData
                                    {
                                        ErrorCode = 1,
                                        Message = $"操作失败！内容包含特殊字符。",
                                        DataList = null
                                    }
                                    );
                                    return Task.CompletedTask;
                                }
                            }
                        }
                    }
                    
                }
                catch (Exception ex)
                {
                    continue;
                }

            }
            return base.OnActionExecutionAsync(context, next);
        }

        //private bool IsExistSpecialChar(Type type, object obj, out string fieldNam)
        //{
        //    var proper = type.GetProperties();
        //    fieldNam = string.Empty;
        //    var flag = false;
        //    foreach (var item in proper)
        //    {
        //        if (!string.IsNullOrWhiteSpace(item.PropertyType.Name) && item.PropertyType.Name.Equals("String"))
        //        {
        //            var val = item.GetValue(obj, null);
        //            //if (StringHelper.IsExistSQLChar(val.ToStr()))
        //            //{
        //            //    fieldNam = item.Name;
        //            //    flag = true;
        //            //}
        //        }
        //        else if (!string.IsNullOrWhiteSpace(item.PropertyType.Name) && item.PropertyType.Name.Contains("Dictionary"))
        //        {
        //            var dictionary = item.GetValue(obj, null) as System.Collections.IDictionary;
        //            foreach (var val in dictionary.Values)
        //            {
        //                if (!string.IsNullOrWhiteSpace(val.GetType().Name) && val.GetType().Name.Equals("String"))
        //                {
        //                    //if (StringHelper.IsExistSQLChar(val.ToStr()))
        //                    //{
        //                    //    fieldNam = item.Name;
        //                    //    flag = true;
        //                    //}
        //                }
        //                else if (!string.IsNullOrWhiteSpace(val.GetType().BaseType.Name) && val.GetType().BaseType.Name.Equals("Object"))
        //                {
        //                    flag = IsExistSpecialChar(val.GetType(), val, out fieldNam);
        //                }
        //                if (flag)
        //                {
        //                    return flag;
        //                }
        //            }
        //        }
        //        else if (!string.IsNullOrWhiteSpace(item.PropertyType.BaseType.Name) && item.PropertyType.BaseType.Name.Equals("Object"))
        //        {
        //            flag = IsExistSpecialChar(item.PropertyType, item.GetValue(obj, null), out fieldNam);
        //        }
        //        if (flag)
        //        {
        //            return flag;
        //        }
        //    }
        //    return flag;
        //}
    }
}

